package homework;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class Test07 {
    //3.由控制台按照固定格式输入学生信息，包括学号，姓名，年龄信息，当输入的内容为exit退出；
    // 将输入的学生信息分别封装到一个Student对象中，再将每个Student对象加入到一个集合中，
    // 要求集合中的元素按照年龄大小正序排序；最后遍历集合，将集合中学生信息写入到记事本，每个学生数据占单独一行。

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        while (true){
            System.out.println("退出输入exit即可");
            System.out.println("请输入学号：");
            String id = sc.next();
            if ("exit".equals(id)){
                break;
            }
            System.out.println("请输入姓名：");
            String name = sc.next();
            if ("exit".equals(name)){
                break;
            }
            System.out.println("请输入年龄：");
            String age = sc.next();
            Student student = new Student(id, name, age);
            list.add(student);
            if("exit".equals(age)){
                break;
            }
        }
        for (int i = 0; i < list.size()-1; i++) {
            if (Integer.parseInt(list.get(i).getAge())>Integer.parseInt(list.get(i+1).getAge())){
                Student temp = list.get(i);
                list.set(i, list.get(i+1));
                list.set(i+1, temp);
            }
        }
        System.out.println(list);
    }
}
class Student implements Serializable ,Comparable<Student>{
    @Serial
    private static final long serialVersionUID = 4826052394138337721L;
    private String id;
    private String name;
    private String age;
    public Student() {
    }

    public Student(String id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(age, student.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        if (Integer.parseInt(this.age) > Integer.parseInt(o.age)){
            return 1;
        }
        return -1;
    }
}
